import image
import time
import data_object
from const import *
class ObjectBase(image.Image):
    def __init__(self,id,pos):
        self.id=id
        self.hp = self.getData()['HP']
        self.attack = self.getData()['ATT']
        self.preIndexTime = 0
        self.prePositionTime = 0
        self.preSummonTime = 0 
        super(ObjectBase, self).__init__(
            self.getData()['PATH'],
            0,
            pos,
            self.getData()['SIZE'],
            self.getData()['IMAGE_INDEX_MAX']
            )
    def isCollide(self, other):
        return self.getRect().colliderect(other.getRect() )
    def canLoot(self):
        return self.getData()['CAN_LOOT']
    def getData(self):
        return data_object.data[self.id]
    def getPositionCD(self):
        return self.getData()['POSITION_CD']
    def getImageIndexCD(self):
        return self.getData()['IMAGE_INDEX_CD']
    def getSummonCD(self):
        return self.getData()['SUMMON_CD']
    def getSpeed(self):
        return self.getData()['SPEED']
    def getPrice(self):
        return self.getData()['PRICE']
    def update(self):
        self.checkImageIndex()
        self.checkPosition()
        self.checkSummon()
    
    def checkImageIndex(self):
        if time.time() - self.preIndexTime <= self.getImageIndexCD():
            return
        self.preIndexTime = time.time()
        if self.pathIndex + 1 >= self.pathIndexCount:
            self.updateIndex(0)
        else :
            self.updateIndex(self.pathIndex + 1)

    def checkPosition(self):
        if time.time() - self.prePositionTime <= self.getPositionCD():
            return False
        self.prePositionTime = time.time()
        speed = self.getSpeed()
        if self.id == SUNLIGHT_ID and self.pos[1]+ speed[1]> BOTTOM:
            return False
        else :
            self.pos = ( self.pos[0] + speed[0], self.pos[1] + speed[1])
        return True

    def checkSummon(self):
        if time.time() - self.preSummonTime <= self.getSummonCD():
            return
        self.preSummonTime = time.time()
        self.preSummon()

    def preSummon(self):
        pass
    
